package com.guldan.thrall.common.filter;

import com.alibaba.dubbo.rpc.*;
import com.guldan.thrall.util.UserUtils;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Silas.
 * Date: 2016/12/10
 * Time: 16:13
 */
public class DubboConsumerFilter implements Filter {


    protected Logger logger = LoggerFactory.getLogger(getClass());


    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {

        //将登录用户信息添加到dubbo调用链中
        if (UserUtils.getUser() != null)
            RpcContext.getContext().setAttachment("guldan-user", JSONObject.fromObject(UserUtils.getUser()).toString());

//        logger.info("guldan-user: " + JSONObject.fromObject(UserUtils.getUser()).toString());

        Result result = invoker.invoke(invocation);
        return result;
    }
}
